package com.wn.class30.service.impl;

import com.wn.class30.entity.Comment;
import com.wn.class30.entity.CommentExample;
import com.wn.class30.entity.CommentImg;
import com.wn.class30.entity.CommentImgExample;
import com.wn.class30.mapper.CommentDao;
import com.wn.class30.mapper.CommentImgDao;
import com.wn.class30.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentDao commentDao;
    @Resource
    private CommentImgDao commentImgDao;
    @Override
    public List<Comment> selectCommponents(Map<String, String> map) throws Exception {
        return commentDao.selectByExample(getCommentExample(map));
    }

    @Override
    public Long selectCommponentCount(Map<String, String> map) throws Exception {
        return commentDao.countByExample(getCommentExample(map));
    }

    @Override
    public List<CommentImg> selectCommentImgsById(Integer commentId) throws Exception {
        CommentImgExample commentImgExample = new CommentImgExample();
        CommentImgExample.Criteria criteria = commentImgExample.createCriteria();
        criteria.andCommentIdEqualTo(commentId);
        return commentImgDao.selectByExample(commentImgExample);
    }

    @Override
    public void updateCommentAnswerById(Comment comment) throws Exception {
        commentDao.updateByPrimaryKey(comment);
    }

    @Override
    public Comment selectCommentById(Integer id) throws Exception {
        return commentDao.selectByPrimaryKey(id);
    }

    @Override
    public void deleteCommentById(Integer id) throws Exception {
        commentDao.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteCommentImgByCommentId(Integer commentId) throws Exception {
        CommentImgExample commentImgExample = new CommentImgExample();
        CommentImgExample.Criteria criteria = commentImgExample.createCriteria();
        criteria.andCommentIdEqualTo(commentId);
        commentImgDao.deleteByExample(commentImgExample);
    }

    public CommentExample getCommentExample(Map<String,String> map)throws Exception{
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        commentExample.setLimit(limit);
        commentExample.setOffset(page);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String isanswer = map.get("isanswer");
        String startPoint = map.get("startPoint");
        String endPoint = map.get("endPoint");
        String businessId = map.get("businessId");
        if(!"".equals(businessId) && businessId != null && Integer.parseInt(businessId) > 0){
            criteria.andBusinessIdEqualTo(Integer.parseInt(map.get("businessId")));
        }
        if(!"".equals(isanswer) && isanswer != null && Integer.parseInt(isanswer) > 0){
            criteria.andIsanswerEqualTo(Integer.parseInt(map.get("isanswer")));
        }
        if(!"".equals(startPoint) && startPoint != null && Integer.parseInt(startPoint) > 0){
            criteria.andCommentPointGreaterThanOrEqualTo(Integer.parseInt(map.get("startPoint")));
        }
        if(!"".equals(endPoint) && endPoint != null && Integer.parseInt(endPoint) > 0){
            criteria.andCommentPointLessThanOrEqualTo(Integer.parseInt(map.get("endPoint")));
        }

        if(!"".equals(startDate) && startDate != null){
            Date date = simpleDateFormat.parse(startDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andCommentTimeGreaterThanOrEqualTo(simpleDateFormat.parse(format));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 2);
            Date time = c.getTime();
            String format = simpleDateFormat.format(time);
            criteria.andCommentTimeLessThanOrEqualTo(simpleDateFormat.parse(format));
        }
        return commentExample;
    }
}
